package learn.java;
import java.util.*;
public class List<T> implements IList<T>
{
    private int size;
    private int capacity;
    private int growNum;
    public  T[] items;
    public List()
    {
        growNum=2;
        size=0;
    }
    public void initialItems(T[] t)
    {
        capacity=t.length;
        items=(T[])new Object[capacity];
        System.arraycopy(t,0,items,0,capacity);
        size=items.length;
    }
    public void resize()
    {
        if(items.length>=capacity)
        {
            capacity=capacity*growNum;
            items= Arrays.copyOf(items, capacity);
        }
    }
    private void rangeCheck(int index)
    {
        if (index < 0 || index >= size)
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
    }
    public  void add(T t)
    {
        resize();
        items[size]=t;
        size++;
    }
    public void delete(int pos)
    {
        rangeCheck(pos);
        int numMoved = size - pos- 1;
        if (numMoved > 0)
            System.arraycopy(items, pos+1, items, pos,
                    numMoved);
        items[--size]=null;
    }
    public  void swap(int pos1,int pos2)
    {
        rangeCheck(pos1);
        rangeCheck(pos2);
        T temp;
        temp=items[pos1];
        items[pos1]=items[pos2];
        items[pos2]=temp;
    }

    public T get(int pos)
    {
        rangeCheck(pos);
        return items[pos];
    }
    public int size()
    {
        return size;
    }
    public void change(T t,int pos)
    {
        rangeCheck(pos);
        items[pos]=t;
    }
    public boolean isEmpty()
    {
        if(size==0)
            return true;
        else
            return false;
    }
    public void testFile()
    {
        Integer[] items={1,2,3,4,5,6};
        List<Integer> demoList=new List<Integer>();
        demoList.initialItems(items);
        demoList.add(10);
        demoList.delete(1);
       demoList.swap(2,3);
        System.out.println(demoList.get(1));
        System.out.println(demoList.isEmpty());
        System.out.println(demoList.size());
    }
    public static void main(String[] args)
    {
        List<Integer> demoList=new List<Integer>();
        demoList.testFile();
    }
}
